<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Python3学习教程</title>
  <meta name="description" content="Python3学习教程">
  <meta name="generator" content="bookdown 0.5 and GitBook 2.6.7">

  <meta property="og:title" content="Python3学习教程" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Python3学习教程" />
  
  
  

<meta name="author" content="易生信培训团队">
<meta name="author" content="联系我们: train@ehbio.com">


<meta name="date" content="2018-05-09">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="python-py3-ct.html">
<link rel="next" href="-1.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="http://www.ehbio.com"><img src="http://www.ehbio.com/logos/ehbio_gitbook_logo.png" width="95%"></a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>EHBIO Gene Technology</a></li>
<li class="chapter" data-level="1" data-path="pythonbasic.html"><a href="pythonbasic.html"><i class="fa fa-check"></i><b>1</b> Python基础</a><ul>
<li class="chapter" data-level="1.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.1"><i class="fa fa-check"></i><b>1.1</b> 交互模式下表达式</a></li>
<li class="chapter" data-level="1.2" data-path="pythonbasic.html"><a href="pythonbasic.html#pythonintfloatstr"><i class="fa fa-check"></i><b>1.2</b> Python中的数据类型：整数（int）、浮点（float）和字符串（str）</a></li>
<li class="chapter" data-level="1.3" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.3"><i class="fa fa-check"></i><b>1.3</b> 字符串的连接和复制</a></li>
<li class="chapter" data-level="1.4" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.4"><i class="fa fa-check"></i><b>1.4</b> 变量</a></li>
<li class="chapter" data-level="1.5" data-path="pythonbasic.html"><a href="pythonbasic.html#helloworld.py"><i class="fa fa-check"></i><b>1.5</b> 第一小程序HelloWorld.py</a><ul>
<li class="chapter" data-level="1.5.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.5.1"><i class="fa fa-check"></i><b>1.5.1</b> 控制流</a></li>
</ul></li>
<li class="chapter" data-level="1.6" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6"><i class="fa fa-check"></i><b>1.6</b> 布尔逻辑值</a></li>
<li class="chapter" data-level="1.7" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.7"><i class="fa fa-check"></i><b>1.7</b> 比较操作符</a></li>
<li class="chapter" data-level="1.8" data-path="pythonbasic.html"><a href="pythonbasic.html#-and-or-not"><i class="fa fa-check"></i><b>1.8</b> 布尔操作符 and or not</a></li>
<li class="chapter" data-level="1.9" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.9"><i class="fa fa-check"></i><b>1.9</b> 布尔操作和比较操作符</a></li>
<li class="chapter" data-level="1.10" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>1.10</b> 控制流</a><ul>
<li class="chapter" data-level="1.10.1" data-path="pythonbasic.html"><a href="pythonbasic.html#if-"><i class="fa fa-check"></i><b>1.10.1</b> if 语句</a></li>
<li class="chapter" data-level="1.10.2" data-path="pythonbasic.html"><a href="pythonbasic.html#elif-"><i class="fa fa-check"></i><b>1.10.2</b> elif 否则如果</a></li>
<li class="chapter" data-level="1.10.3" data-path="pythonbasic.html"><a href="pythonbasic.html#while"><i class="fa fa-check"></i><b>1.10.3</b> while循环</a></li>
<li class="chapter" data-level="1.10.4" data-path="pythonbasic.html"><a href="pythonbasic.html#breakcontinue"><i class="fa fa-check"></i><b>1.10.4</b> break和continue</a></li>
<li class="chapter" data-level="1.10.5" data-path="pythonbasic.html"><a href="pythonbasic.html#for--range"><i class="fa fa-check"></i><b>1.10.5</b> for 和 range（）函数</a></li>
<li class="chapter" data-level="1.10.6" data-path="pythonbasic.html"><a href="pythonbasic.html#range"><i class="fa fa-check"></i><b>1.10.6</b> range()函数（开始，停止，步长）</a></li>
</ul></li>
<li class="chapter" data-level="1.11" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.11"><i class="fa fa-check"></i><b>1.11</b> 导入模块</a><ul>
<li class="chapter" data-level="1.11.1" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>1.11.1</b> 函数： 内置函数、自定义函数</a></li>
</ul></li>
<li class="chapter" data-level="1.12" data-path="pythonbasic.html"><a href="pythonbasic.html#printlen-input-intstr"><i class="fa fa-check"></i><b>1.12</b> 函数print（），len （）,input （），int（），str（）均为内置函数</a></li>
<li class="chapter" data-level="1.13" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.13"><i class="fa fa-check"></i><b>1.13</b> 局部和全局作用域</a></li>
<li class="chapter" data-level="1.14" data-path="pythonbasic.html"><a href="pythonbasic.html#global"><i class="fa fa-check"></i><b>1.14</b> 声明为全局变量global</a></li>
<li class="chapter" data-level="1.15" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.15"><i class="fa fa-check"></i><b>1.15</b> 异常的处理</a><ul>
<li class="chapter" data-level="1.15.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.15.1"><i class="fa fa-check"></i><b>1.15.1</b> 列表</a></li>
</ul></li>
<li class="chapter" data-level="1.16" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.16"><i class="fa fa-check"></i><b>1.16</b> 取列表中单个值</a></li>
<li class="chapter" data-level="1.17" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.17"><i class="fa fa-check"></i><b>1.17</b> 类似列表的类型：字符串和元组</a></li>
<li class="chapter" data-level="1.18" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.18"><i class="fa fa-check"></i><b>1.18</b> 引用</a><ul>
<li class="chapter" data-level="1.18.1" data-path="pythonbasic.html"><a href="pythonbasic.html#--"><i class="fa fa-check"></i><b>1.18.1</b> 字典 键：值 对</a></li>
</ul></li>
<li class="chapter" data-level="1.19" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.19"><i class="fa fa-check"></i><b>1.19</b> 字典与列表</a></li>
<li class="chapter" data-level="1.20" data-path="pythonbasic.html"><a href="pythonbasic.html#keysvaluesitems"><i class="fa fa-check"></i><b>1.20</b> keys()、values（）和items( )</a><ul>
<li class="chapter" data-level="1.20.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.20.1"><i class="fa fa-check"></i><b>1.20.1</b> 字符串操作</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="python-py3-ct.html"><a href="python-py3-ct.html"><i class="fa fa-check"></i><b>2</b> Python 教程 {py3_ct}</a><ul>
<li class="chapter" data-level="2.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.1"><i class="fa fa-check"></i><b>2.1</b> 背景介绍</a><ul>
<li class="chapter" data-level="2.1.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.1.1"><i class="fa fa-check"></i><b>2.1.1</b> 编程开篇</a></li>
<li class="chapter" data-level="2.1.2" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.2</b> 为什么学习Python</a></li>
<li class="chapter" data-level="2.1.3" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.3</b> Python常用包</a></li>
<li class="chapter" data-level="2.1.4" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.4</b> 怎么学习Python</a></li>
<li class="chapter" data-level="2.1.5" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.5</b> Python学习的几个阶段</a></li>
<li class="chapter" data-level="2.1.6" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.6</b> 如何安装Python</a></li>
<li class="chapter" data-level="2.1.7" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.7</b> 如何运行Python命令和脚本</a></li>
<li class="chapter" data-level="2.1.8" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.8</b> 使用什么编辑器写Python脚本</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.2</b> Python程序事例</a></li>
<li class="chapter" data-level="2.3" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.3</b> Python语法</a><ul>
<li class="chapter" data-level="2.3.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.3.1"><i class="fa fa-check"></i><b>2.3.1</b> 层级缩进</a></li>
<li class="chapter" data-level="2.3.2" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.3.2</b> Python作为计算器的使用</a></li>
<li class="chapter" data-level="2.3.3" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.3.3"><i class="fa fa-check"></i><b>2.3.3</b> 变量、数据结构、流程控制</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.4"><i class="fa fa-check"></i><b>2.4</b> 输入输出</a><ul>
<li class="chapter" data-level="2.4.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.4.1"><i class="fa fa-check"></i><b>2.4.1</b> 交互式输入输出</a></li>
<li class="chapter" data-level="2.4.2" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.4.2"><i class="fa fa-check"></i><b>2.4.2</b> 文件读写</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.5"><i class="fa fa-check"></i><b>2.5</b> 实战练习（一）</a><ul>
<li class="chapter" data-level="2.5.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.5.1"><i class="fa fa-check"></i><b>2.5.1</b> 背景知识</a></li>
<li class="chapter" data-level="2.5.2" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>2.5.2</b> 作业 (一)</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.6"><i class="fa fa-check"></i><b>2.6</b> 函数操作</a><ul>
<li class="chapter" data-level="2.6.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.6.1"><i class="fa fa-check"></i><b>2.6.1</b> 作业（二）</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.7"><i class="fa fa-check"></i><b>2.7</b> 模块</a></li>
<li class="chapter" data-level="2.8" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.8"><i class="fa fa-check"></i><b>2.8</b> 命令行参数</a><ul>
<li class="chapter" data-level="2.8.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.8.1"><i class="fa fa-check"></i><b>2.8.1</b> 作业（三）</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.9</b> 更多Python内容</a><ul>
<li class="chapter" data-level="2.9.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.9.1"><i class="fa fa-check"></i><b>2.9.1</b> <strong>单语句块</strong></a></li>
<li class="chapter" data-level="2.9.2" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.9.2"><i class="fa fa-check"></i><b>2.9.2</b> 列表解析</a></li>
<li class="chapter" data-level="2.9.3" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.9.3"><i class="fa fa-check"></i><b>2.9.3</b> 字典解析</a></li>
<li class="chapter" data-level="2.9.4" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.9.4"><i class="fa fa-check"></i><b>2.9.4</b> 断言</a></li>
<li class="chapter" data-level="2.9.5" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.9.5"><i class="fa fa-check"></i><b>2.9.5</b> 更多字符串方法</a></li>
<li class="chapter" data-level="2.9.6" data-path="python-py3-ct.html"><a href="python-py3-ct.html#lambda-map-filer-reduce-"><i class="fa fa-check"></i><b>2.9.6</b> lambda, map, filer, reduce (保留节目)</a></li>
<li class="chapter" data-level="2.9.7" data-path="python-py3-ct.html"><a href="python-py3-ct.html#exec-eval-python-"><i class="fa fa-check"></i><b>2.9.7</b> <strong>exec, eval (执行字符串python语句, 保留节目)</strong></a></li>
<li class="chapter" data-level="2.9.8" data-path="python-py3-ct.html"><a href="python-py3-ct.html#section-2.9.8"><i class="fa fa-check"></i><b>2.9.8</b> 正则表达式</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>2.10</b> Python画图</a></li>
<li class="chapter" data-level="2.11" data-path="python-py3-ct.html"><a href="python-py3-ct.html#reference"><i class="fa fa-check"></i><b>2.11</b> Reference</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="py3-file.html"><a href="py3-file.html"><i class="fa fa-check"></i><b>3</b> 文件操作</a><ul>
<li class="chapter" data-level="3.1" data-path="py3-file.html"><a href="py3-file.html#section-3.1"><i class="fa fa-check"></i><b>3.1</b> 打开文件</a></li>
<li class="chapter" data-level="3.2" data-path="py3-file.html"><a href="py3-file.html#x"><i class="fa fa-check"></i><b>3.2</b> 使用x模式避免重新写入文件</a></li>
<li class="chapter" data-level="3.3" data-path="py3-file.html"><a href="py3-file.html#section-3.3"><i class="fa fa-check"></i><b>3.3</b> 使用异常处理</a></li>
<li class="chapter" data-level="3.4" data-path="py3-file.html"><a href="py3-file.html#read"><i class="fa fa-check"></i><b>3.4</b> 使用read()读入文本数据</a></li>
<li class="chapter" data-level="3.5" data-path="py3-file.html"><a href="py3-file.html#readline"><i class="fa fa-check"></i><b>3.5</b> 使用readline()分行读取文件</a></li>
<li class="chapter" data-level="3.6" data-path="py3-file.html"><a href="py3-file.html#section-3.6"><i class="fa fa-check"></i><b>3.6</b> 使用迭代器分行读取文件</a></li>
<li class="chapter" data-level="3.7" data-path="py3-file.html"><a href="py3-file.html#readlines"><i class="fa fa-check"></i><b>3.7</b> 用readlines()函数返回单行字符串的列表</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>4</b> 正则表达式</a></li>
<li class="chapter" data-level="5" data-path="section-5.html"><a href="section-5.html"><i class="fa fa-check"></i><b>5</b> 习题</a><ul>
<li class="chapter" data-level="5.1" data-path="section-5.html"><a href="section-5.html#gc"><i class="fa fa-check"></i><b>5.1</b> 计算大肠杆菌基因组的GC含量</a></li>
<li class="chapter" data-level="5.2" data-path="section-5.html"><a href="section-5.html#p53cdnarna"><i class="fa fa-check"></i><b>5.2</b> 把人的P53的cDNA序列转录为RNA，再翻译为蛋白质</a></li>
<li class="chapter" data-level="5.3" data-path="section-5.html"><a href="section-5.html#seqs.fa"><i class="fa fa-check"></i><b>5.3</b> 作业：求取文件seqs.fa中多条序列的反向互补序列</a></li>
<li class="chapter" data-level="5.4" data-path="section-5.html"><a href="section-5.html#seci"><i class="fa fa-check"></i><b>5.4</b> 列出大肠杆菌基因组中限制性内切酶SecI的切割位置</a></li>
<li class="chapter" data-level="5.5" data-path="section-5.html"><a href="section-5.html#tp53-mrnaorf"><i class="fa fa-check"></i><b>5.5</b> 作业：找出TP53 mRNA序列中的ORF</a></li>
<li class="chapter" data-level="5.6" data-path="section-5.html"><a href="section-5.html#fasta"><i class="fa fa-check"></i><b>5.6</b> FASTA文件处理</a></li>
<li class="chapter" data-level="5.7" data-path="section-5.html"><a href="section-5.html#fatsq"><i class="fa fa-check"></i><b>5.7</b> 作业：FATSQ文件处理</a></li>
<li class="chapter" data-level="5.8" data-path="section-5.html"><a href="section-5.html#json"><i class="fa fa-check"></i><b>5.8</b> JSON文件解析</a></li>
<li class="chapter" data-level="5.9" data-path="section-5.html"><a href="section-5.html#genbankcds"><i class="fa fa-check"></i><b>5.9</b> GenBank文件解析，提取CDS序列</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html"><i class="fa fa-check"></i><b>6</b> Python作图 {Python_plot_WXN}</a><ul>
<li class="chapter" data-level="6.1" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html#section-6.1"><i class="fa fa-check"></i><b>6.1</b> 绘图基础</a><ul>
<li class="chapter" data-level="6.1.1" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html#figuresubplot-1"><i class="fa fa-check"></i><b>6.1.1</b> Figure和Subplot</a></li>
<li class="chapter" data-level="6.1.2" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html#subplot-1"><i class="fa fa-check"></i><b>6.1.2</b> 调整subplot周围间距</a></li>
<li class="chapter" data-level="6.1.3" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html#--1"><i class="fa fa-check"></i><b>6.1.3</b> 颜色 标记和线型</a></li>
<li class="chapter" data-level="6.1.4" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.1.4</b> 刻度、标签和图例</a></li>
<li class="chapter" data-level="6.1.5" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html#legend-1"><i class="fa fa-check"></i><b>6.1.5</b> 添加图例legend</a></li>
<li class="chapter" data-level="6.1.6" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.1.6</b> 注解</a></li>
<li class="chapter" data-level="6.1.7" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.1.7</b> 图片保存</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html#section-6.2"><i class="fa fa-check"></i><b>6.2</b> 绘图实例</a><ul>
<li class="chapter" data-level="6.2.1" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.2.1</b> 绘制散点图</a></li>
<li class="chapter" data-level="6.2.2" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.2.2</b> 折线图</a></li>
<li class="chapter" data-level="6.2.3" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.2.3</b> 直方图</a></li>
<li class="chapter" data-level="6.2.4" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.2.4</b> 直条图</a></li>
<li class="chapter" data-level="6.2.5" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.2.5</b> 箱线图</a></li>
<li class="chapter" data-level="6.2.6" data-path="pythonbasic.html"><a href="pythonbasic.html#-1"><i class="fa fa-check"></i><b>6.2.6</b> 饼图</a></li>
<li class="chapter" data-level="6.2.7" data-path="python-python-plot-wxn.html"><a href="python-python-plot-wxn.html#section-6.2.7"><i class="fa fa-check"></i><b>6.2.7</b> 绘制基因矩阵的热图</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html"><i class="fa fa-check"></i><b>7</b> Python实战</a><ul>
<li class="chapter" data-level="7.1" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>7.1</b> Python实战</a><ul>
<li class="chapter" data-level="7.1.1" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#id"><i class="fa fa-check"></i><b>7.1.1</b> ID转换</a></li>
<li class="chapter" data-level="7.1.2" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#section-7.1.2"><i class="fa fa-check"></i><b>7.1.2</b> 每条染色体基因数目统计</a></li>
<li class="chapter" data-level="7.1.3" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#section-7.1.3"><i class="fa fa-check"></i><b>7.1.3</b> 所有外显子总长度统计</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="python-py3-ct.html"><a href="python-py3-ct.html#python"><i class="fa fa-check"></i><b>7.2</b> Python小技巧</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html"><i class="fa fa-check"></i><b>8</b> Pandas 学习教程</a><ul>
<li class="chapter" data-level="8.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#what-is-pandas"><i class="fa fa-check"></i><b>8.1</b> What is pandas</a></li>
<li class="chapter" data-level="8.2" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>8.2</b> Pandas读取文件</a><ul>
<li class="chapter" data-level="8.2.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.1"><i class="fa fa-check"></i><b>8.2.1</b> 获取目标文件</a></li>
<li class="chapter" data-level="8.2.2" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.2"><i class="fa fa-check"></i><b>8.2.2</b> 查看目标文件内容和格式</a></li>
<li class="chapter" data-level="8.2.3" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.3"><i class="fa fa-check"></i><b>8.2.3</b> 读取两列文件</a></li>
<li class="chapter" data-level="8.2.4" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.4"><i class="fa fa-check"></i><b>8.2.4</b> 数据表的索引</a></li>
<li class="chapter" data-level="8.2.5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.5"><i class="fa fa-check"></i><b>8.2.5</b> 读取多列文件</a></li>
<li class="chapter" data-level="8.2.6" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.6"><i class="fa fa-check"></i><b>8.2.6</b> 选取多列数据</a></li>
<li class="chapter" data-level="8.2.7" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.7"><i class="fa fa-check"></i><b>8.2.7</b> 重命名列名字</a></li>
<li class="chapter" data-level="8.2.8" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.8"><i class="fa fa-check"></i><b>8.2.8</b> 合并矩阵</a></li>
<li class="chapter" data-level="8.2.9" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.2.9"><i class="fa fa-check"></i><b>8.2.9</b> 矩阵数据提取</a></li>
<li class="chapter" data-level="8.2.10" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#meta-data"><i class="fa fa-check"></i><b>8.2.10</b> 读取META data文件</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>8.3</b> Pandas写入文件</a><ul>
<li class="chapter" data-level="8.3.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-8.3.1"><i class="fa fa-check"></i><b>8.3.1</b> 写入文本文件</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>8.4</b> PANDAS矩阵的小应用</a></li>
<li class="chapter" data-level="8.5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#seaborn"><i class="fa fa-check"></i><b>8.5</b> Seaborn绘图</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="Py3-test.html"><a href="Py3-test.html"><i class="fa fa-check"></i><b>9</b> 易生信Python培训练习和考核题目</a></li>
<li class="chapter" data-level="10" data-path="numpy-scipy.html"><a href="numpy-scipy.html"><i class="fa fa-check"></i><b>10</b> Python科学计算</a><ul>
<li class="chapter" data-level="10.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>10.1</b> NumPy</a><ul>
<li class="chapter" data-level="10.1.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>10.1.1</b> NumPy数组</a></li>
<li class="chapter" data-level="10.1.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>10.1.2</b> 布尔语句和NumPy数组</a></li>
<li class="chapter" data-level="10.1.3" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>10.1.3</b> NumPy读写文件</a></li>
<li class="chapter" data-level="10.1.4" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpymath"><i class="fa fa-check"></i><b>10.1.4</b> NumPy的Math模块</a></li>
</ul></li>
<li class="chapter" data-level="10.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#scipy"><i class="fa fa-check"></i><b>10.2</b> SciPy</a><ul>
<li class="chapter" data-level="10.2.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#section-10.2.1"><i class="fa fa-check"></i><b>10.2.1</b> 最优化和最小化</a></li>
<li class="chapter" data-level="10.2.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#section-10.2.2"><i class="fa fa-check"></i><b>10.2.2</b> 插值</a></li>
</ul></li>
</ul></li>
<li class="divider"></li>
<li><a href="mailto:ct@ehbio.com" target="blank">ct@ehbio.com</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Python3学习教程</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="py3_file" class="section level1">
<h1><span class="header-section-number">3</span> 文件操作</h1>
<p>欢迎访问我们的视频课程 <a href="https://bioinfo.ke.qq.com" class="uri">https://bioinfo.ke.qq.com</a>。</p>
<div id="section-3.1" class="section level2">
<h2><span class="header-section-number">3.1</span> 打开文件</h2>
<p>fileobj = open(filename, mode)</p>
<ul>
<li>fileobj是open()函数返回的文件对象</li>
<li>filename是文件的字符串名</li>
<li><p>mode指明操作和文件类型</p></li>
<li>操作：r表示读模式，w表示写模式（文件不存在则创建，文件存在则清空重写），a表示追加（若文件存在，在文件末尾追加写入）、x表示文件不存在时创建新文件写入（文件存在则报错）</li>
<li><p>文件类型：默认为t，t表示以文本文件方式操作，b表示以二进制文件方式操作</p></li>
</ul>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">kinase_seq<span class="op">=</span><span class="st">&#39;&#39;&#39;&gt;NAD kinase isoform 1 [Homo sapiens]</span>
<span class="st">MEMEQEKMTMNKELSPDAAAYCCSACHGDETWSYNHPIRGRAKSRSLSASPALGSTKEFRRTRSLHGPCP</span>
<span class="st">VTTFGPKACVLQNPQTIMHIQDPASQRLTWNKSPKSVLVIKKMRDASLLQPFKELCTHLMEENMIVYVEK</span>
<span class="st">KVLEDPAIASDESFGAVKKKFCTFREDYDDISNQIDFIICLGGDGTLLYASSLFQGSVPPVMAFHLGSLG</span>
<span class="st">FLTPFSFENFQSQVTQVIEGNAAVVLRSRLKVRVVKELRGKKTAVHNGLGENGSQAAGLDMDVGKQAMQY</span>
<span class="st">QVLNEVVIDRGPSSYLSNVDVYLDGHLITTVQGDGVIVSTPTGSTAYAAAAGASMIHPNVPAIMITPICP</span>
<span class="st">HSLSFRPIVVPAGVELKIMLSPEARNTAWVSFDGRKRQEIRHGDSISITTSCYPLPSICVRDPVSDWFES</span>
<span class="st">LAQCLHWNVRKKQAHFEEEEEEEEEG&#39;&#39;&#39;</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 使用write语句写入文件</span>
fout<span class="op">=</span><span class="bu">open</span>(<span class="st">&quot;protein_seq_1.fa&quot;</span>, <span class="st">&quot;wt&quot;</span>)
fout.write(kinase_seq)
fout.close()</code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 或者可以使用print语句</span>
fout<span class="op">=</span><span class="bu">open</span>(<span class="st">&quot;protein_seq_2.fa&quot;</span>, <span class="st">&quot;wt&quot;</span>)
<span class="co"># print函数的参数sep和end，默认为sep=&quot; &quot;, end=&quot;\n&quot;</span>
<span class="bu">print</span>(kinase_seq, <span class="bu">file</span><span class="op">=</span>fout, sep<span class="op">=</span><span class="st">&quot;&quot;</span>, end<span class="op">=</span><span class="st">&quot;&quot;</span>)
fout.close()</code></pre></div>
</div>
<div id="x" class="section level2">
<h2><span class="header-section-number">3.2</span> 使用x模式避免重新写入文件</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># fout = open(&quot;protein_seq_1.fa&quot;, &quot;xt&quot;)</span></code></pre></div>
</div>
<div id="section-3.3" class="section level2">
<h2><span class="header-section-number">3.3</span> 使用异常处理</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">try</span>:
    fout <span class="op">=</span> <span class="bu">open</span>(<span class="st">&quot;protein_seq_1.fa&quot;</span>, <span class="st">&quot;xt&quot;</span>)
    fout.write(<span class="st">&quot;stomp stomp stomp&quot;</span>)
<span class="cf">except</span> <span class="pp">FileExistsError</span>:
    <span class="bu">print</span>(<span class="st">&quot;The file protein_seq_1.fa already exists!&quot;</span>)</code></pre></div>
<pre><code>The file protein_seq_1.fa already exists!</code></pre>
</div>
<div id="read" class="section level2">
<h2><span class="header-section-number">3.4</span> 使用read()读入文本数据</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">fin <span class="op">=</span> <span class="bu">open</span>(<span class="st">&quot;protein_seq_1.fa&quot;</span>, <span class="st">&quot;rt&quot;</span>)
kinase_seq <span class="op">=</span> fin.read()
fin.close()
<span class="bu">len</span>(kinase_seq)</code></pre></div>
<pre><code>489</code></pre>
</div>
<div id="readline" class="section level2">
<h2><span class="header-section-number">3.5</span> 使用readline()分行读取文件</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">kinase_seq<span class="op">=</span><span class="st">&quot;&quot;</span>
fin<span class="op">=</span><span class="bu">open</span>(<span class="st">&quot;protein_seq_1.fa&quot;</span>, <span class="st">&quot;rt&quot;</span>)
<span class="cf">while</span> <span class="va">True</span>:
    fragment<span class="op">=</span>fin.readline()
    <span class="cf">if</span> <span class="op">not</span> fragment:
        <span class="cf">break</span>
    kinase_seq <span class="op">+=</span> fragment
fin.close()
<span class="bu">len</span>(kinase_seq)</code></pre></div>
<pre><code>489</code></pre>
</div>
<div id="section-3.6" class="section level2">
<h2><span class="header-section-number">3.6</span> 使用迭代器分行读取文件</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">kinase_seq <span class="op">=</span> <span class="st">&quot;&quot;</span>
fin <span class="op">=</span> <span class="bu">open</span>(<span class="st">&quot;protein_seq_1.fa&quot;</span>, <span class="st">&quot;rt&quot;</span>)
<span class="cf">for</span> line <span class="op">in</span> fin:
    kinase_seq <span class="op">+=</span> line
fin.close()
<span class="bu">len</span>(kinase_seq)</code></pre></div>
<pre><code>489</code></pre>
</div>
<div id="readlines" class="section level2">
<h2><span class="header-section-number">3.7</span> 用readlines()函数返回单行字符串的列表</h2>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">fin <span class="op">=</span> <span class="bu">open</span>(<span class="st">&quot;protein_seq_1.fa&quot;</span>, <span class="st">&quot;rt&quot;</span>)
lines <span class="op">=</span> fin.readlines()
fin.close()
<span class="bu">print</span>(lines)</code></pre></div>
<pre><code>[&#39;&gt;NAD kinase isoform 1 [Homo sapiens]\n&#39;, &#39;MEMEQEKMTMNKELSPDAAAYCCSACHGDETWSYNHPIRGRAKSRSLSASPALGSTKEFRRTRSLHGPCP\n&#39;, &#39;VTTFGPKACVLQNPQTIMHIQDPASQRLTWNKSPKSVLVIKKMRDASLLQPFKELCTHLMEENMIVYVEK\n&#39;, &#39;KVLEDPAIASDESFGAVKKKFCTFREDYDDISNQIDFIICLGGDGTLLYASSLFQGSVPPVMAFHLGSLG\n&#39;, &#39;FLTPFSFENFQSQVTQVIEGNAAVVLRSRLKVRVVKELRGKKTAVHNGLGENGSQAAGLDMDVGKQAMQY\n&#39;, &#39;QVLNEVVIDRGPSSYLSNVDVYLDGHLITTVQGDGVIVSTPTGSTAYAAAAGASMIHPNVPAIMITPICP\n&#39;, &#39;HSLSFRPIVVPAGVELKIMLSPEARNTAWVSFDGRKRQEIRHGDSISITTSCYPLPSICVRDPVSDWFES\n&#39;, &#39;LAQCLHWNVRKKQAHFEEEEEEEEEG&#39;]</code></pre>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="python-py3-ct.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="-1.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": false,
"twitter": false,
"google": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"download": ["Py3_course.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
